package com.zhiwei.msf.infra.oauth.service.impl;

import com.zhiwei.msf.infra.oauth.entity.UserInfo;
import com.zhiwei.msf.infra.oauth.mapper.UserInfoMapper;
import com.zhiwei.msf.infra.oauth.service.UserInfoService;
import com.zhiwei.msf.infra.oauth.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo getByUsername(String username) {
        if(StringUtils.isEmpty(username)){
            return null;
        }
        return userInfoMapper.getByUsername(username);
    }

    @Override
    public boolean addUser(UserInfo userInfo) {
        userInfo.setUuid(UUIDUtil.generate());
        return userInfoMapper.addUser(userInfo) == 1;
    }

    @Override
    public boolean deleteUser(String userId) {
        if(StringUtils.isEmpty(userId)){
            return false;
        }
        return userInfoMapper.deleteUser(userId) == 1;
    }

    @Override
    public boolean updateUser(UserInfo userInfo) {
        if(StringUtils.isEmpty(userInfo.getUuid())){
            return false;
        }
        return userInfoMapper.updateUser(userInfo) == 1;
    }

    @Override
    public UserInfo queryUser(String userId) {
        if(StringUtils.isEmpty(userId)){
            return null;
        }
        return userInfoMapper.getByUserId(userId);
    }
}